{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.数字的四舍五入\n",
    "\n",
    "- 简单的运算，可使用内置方法`round(value, ndigits)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.2"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "1.3"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "-1.3"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "1.254"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(2.0, 2.0)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(1627730, 1627700, 1628000)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(1.23, 1)\n",
    "round(1.27, 1)\n",
    "round(-1.27, 1)\n",
    "round(1.25361,3)\n",
    "\n",
    "# 当一个值刚好在两个边界的中间的时候， round 函数返回离它最近的偶数。 也就是说，对1.5或者2.5的舍入运算都会得到2。\n",
    "round(1.5, 0), round(2.5, 0)\n",
    "\n",
    "# 传给 round() 函数的 ndigits 参数可以是负数，这种情况下， 舍入运算会作用在十位、百位、千位等上面。\n",
    "a = 1627731\n",
    "round(a, -1), round(a, -2), round(a, -3), "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.执行精确的浮点数运算\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.300000000000001"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "Decimal('6.300000000000000266453525910')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "Decimal('6.3')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 浮点数的一个普遍问题是它们并不能精确的表示十进制数。\n",
    "# 这些错误是由底层CPU和IEEE 754标准通过自己的浮点单位去执行算术时的特征。\n",
    "4.2 + 2.1\n",
    "\n",
    "# 可以使用 decimal 模块\n",
    "from decimal import Decimal\n",
    "\n",
    "Decimal(4.2) + Decimal(2.1)\n",
    "Decimal('4.2') + Decimal('2.1')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.7647058823529411764705882353')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "Decimal('0.765')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "Decimal('0.76470588235294117647058823529411764705882352941176')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from decimal import Decimal\n",
    "from decimal import localcontext\n",
    "\n",
    "a = Decimal('1.3')\n",
    "b = Decimal('1.7')\n",
    "\n",
    "a/b\n",
    "\n",
    "# 控制计算的每一方面，包括数字位数和四舍五入运算\n",
    "with localcontext() as ctx:\n",
    "    ctx.prec = 3\n",
    "    a/b\n",
    "\n",
    "with localcontext() as ctx:\n",
    "    ctx.prec = 50\n",
    "    a/b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.数字的格式化输出\n",
    "\n",
    "你需要将数字格式化后输出，并控制数字的位数、对齐、千位分隔符和其他的细节。\n",
    "\n",
    "依然可以使用内置的 format() 函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The value is 1,234.57'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'The value is             1,234.57'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 同时指定宽度和精度的一般形式是 '[<>^]?width[,]?(.digits)?' ， 其中 width 和 digits 为整数，？代表可选部分。\n",
    "\n",
    "x = 1234.56789\n",
    "'The value is {:0,.2f}'.format(x)\n",
    "\n",
    "'The value is {:20,.2f}'.format(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.二八十六进制整数\n",
    "\n",
    "为了将整数转换为二进制、八进制或十六进制的文本串， 可以分别使用 bin() , oct() 或 hex() 函数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0b10011010010'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'0o2322'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'0x4d2'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 1234\n",
    "\n",
    "bin(x)\n",
    "oct(x)\n",
    "hex(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.字节到大整数的打包与解包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "69120565665751139577663547927094891008"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "94522842520747284487117727783387188"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "117"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "b'4\\x00#\\x00\\x01\\xef\\xcd\\x00\\xab\\x90x\\x00V4\\x12\\x00'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "b'\\x00\\x124V\\x00x\\x90\\xab\\x00\\xcd\\xef\\x01\\x00#\\x004'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 你的程序需要处理一个拥有128位长的16个元素的字节字符串。比如：\n",
    "data = b'\\x00\\x124V\\x00x\\x90\\xab\\x00\\xcd\\xef\\x01\\x00#\\x004'\n",
    "\n",
    "int.from_bytes(data, 'little')\n",
    "int.from_bytes(data, 'big')\n",
    "\n",
    "x = 94522842520747284487117727783387188\n",
    "x.bit_length()\n",
    "x.to_bytes(16, 'little')\n",
    "x.to_bytes(16, 'big')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.复数的数学运算\n",
    "\n",
    "复数可以用使用函数 complex(real, imag) 或者是带有后缀j的浮点数来指定。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2+4j)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(3-5j)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(5-1j)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(2.0, 4.0, (2-4j))"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = complex(2, 4)\n",
    "b = 3 - 5j\n",
    "\n",
    "a\n",
    "b\n",
    "a+b\n",
    "\n",
    "print('-'*20)\n",
    "a.real, a.imag, a.conjugate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.无穷大与NaN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(inf, -inf, nan)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Python并没有特殊的语法来表示这些特殊的浮点值，但是可以使用 float() 来创建它们\n",
    "a = float('inf')\n",
    "b = float('-inf')\n",
    "c = float('nan')\n",
    "a, b, c\n",
    "\n",
    "print('-'*20)\n",
    "import math\n",
    "math.isinf(a)\n",
    "math.isfinite(b)\n",
    "math.isnan(c)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(False, False, True)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "# NaN值会在所有操作中传播，而不会产生异常\n",
    "d = float('nan')\n",
    "d + 23\n",
    "d / 2\n",
    "d * 2\n",
    "math.sqrt(d)\n",
    "\n",
    "# NaN值的一个特别的地方时它们之间的比较操作总是返回False\n",
    "e = float('nan')\n",
    "f = float('nan')\n",
    "\n",
    "e == f, e is f, math.isnan(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.分数运算\n",
    "\n",
    "fractions 模块可以被用来执行包含分数的数学运算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(27, 16)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "Fraction(35, 64)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(35, 64)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "0.546875"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "Fraction(4, 7)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from fractions import Fraction\n",
    "\n",
    "a = Fraction(5 ,4)\n",
    "b = Fraction(7, 16)\n",
    "a + b\n",
    "\n",
    "# Getting numerator/denominator\n",
    "c = a * b\n",
    "c\n",
    "c.numerator, c.denominator\n",
    "\n",
    "float(c)\n",
    "# 不懂 limit_denominator 是做什么的\n",
    "c.limit_denominator(8)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(15, 4)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Converting a float to a fraction\n",
    "from fractions import Fraction\n",
    "x = 3.75\n",
    "Fraction(*x.as_integer_ratio())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9.大型数组运算\n",
    "\n",
    "使用 NumPy 库。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.矩阵与线性代数运算\n",
    "\n",
    "还是要使用 NumPy 库。\n",
    "\n",
    "NumPy 库有一个矩阵对象可以用来解决这个问题。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 11.随机选择\n",
    "\n",
    "random 模块有大量的函数用来产生随机数和随机选择元素。\n",
    "\n",
    "- random.choice()\n",
    "- random.sample()\n",
    "- random.shuffle()\n",
    "- random.randint()\n",
    "- random.random()\n",
    "- random.getrandbits()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[3, 2]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[4, 2, 6, 5]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[6, 5, 2, 4, 3, 1]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[3, 4, 6, 5, 2, 1]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "# 从一个序列中随机的抽取一个元素\n",
    "values =  [1, 2, 3, 4, 5, 6]\n",
    "random.choice(values)\n",
    "random.choice(values)\n",
    "random.choice(values)\n",
    "\n",
    "# 提取出N个不同元素的样本\n",
    "random.sample(values, 2)\n",
    "random.sample(values, 4)\n",
    "\n",
    "print('-'*20)\n",
    "# 打乱序列中元素的顺序\n",
    "random.shuffle(values)\n",
    "values\n",
    "random.shuffle(values)\n",
    "values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 12.基本的日期与时间转换\n",
    "\n",
    "为了执行不同时间单位的转换和计算，请使用 datetime 模块。 \n",
    "\n",
    "为了表示一个指定的日期和时间，可以创建一个 datetime 实例。\n",
    "\n",
    "为了表示一个时间段，可以创建一个 timedelta 实例。\n",
    "\n",
    "如果需要执行更加复杂的日期操作，比如处理时区，模糊时间范围，节假日计算等等， 可以考虑使用 dateutil 模块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(days=89)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(2, 37800)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(10.5, 58.5)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2022, 6, 11, 0, 0)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(days=63)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "63"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(datetime.datetime(2022, 6, 29, 16, 0, 24, 343779),\n",
       " datetime.datetime(2022, 6, 29, 16, 10, 24, 343779))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import timedelta\n",
    "from datetime import datetime\n",
    "\n",
    "timedelta(89)\n",
    "a = timedelta(days=2, hours=6)\n",
    "b = timedelta(hours=4.5)\n",
    "c = a+b\n",
    "\n",
    "c.days, c.seconds\n",
    "c.seconds/3600, c.total_seconds()/3600\n",
    "\n",
    "print('-'*20)\n",
    "\n",
    "a = datetime(2022, 6, 1)\n",
    "a + timedelta(days=10)\n",
    "b = datetime(2022, 8, 3)\n",
    "b - a\n",
    "(b - a).days\n",
    "\n",
    "now = datetime.today()\n",
    "now, now+timedelta(minutes=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 13.计算上一个周五的日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2022, 6, 29, 16, 7, 53, 783522)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2022, 7, 1, 16, 7, 53, 783522)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2022, 6, 24, 16, 7, 53, 783522)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "from dateutil.relativedelta import relativedelta\n",
    "import dateutil.rrule as rrule\n",
    "\n",
    "d = datetime.now()\n",
    "d\n",
    "\n",
    "# 下一个周五的日期\n",
    "d + relativedelta(weekday=rrule.FR)\n",
    "\n",
    "# 上一个周五的日期\n",
    "d + relativedelta(weekday=rrule.FR(-1))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 14.计算当前月份的日期范围\n",
    "\n",
    "如：需要在当前月份中循环每一天。\n",
    "\n",
    "解决方案：\n",
    "\n",
    "在这样的日期上循环并需要事先构造一个包含所有日期的列表。 你可以先计算出开始日期和结束日期， 然后在你步进的时候使用 datetime.timedelta 对象递增这个日期变量即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime, timedelta, date\n",
    "import calendar\n",
    "\n",
    "def get_month_range(start_date=None):\n",
    "    if start_date is None:\n",
    "        start_date = date.today().replace(day=1)\n",
    "    _, days_in_month = calendar.monthrange(start_date.year, start_date.month)\n",
    "    end_date = start_date + timedelta(days=days_in_month)\n",
    "    return start_date, end_date\n",
    "\n",
    "def date_range(start, stop, step):\n",
    "    while start < stop:\n",
    "        yield start\n",
    "        start += step\n",
    "\n",
    "# 获取当前月份的第一天、最后一天\n",
    "first_day, last_day = get_month_range()\n",
    "for day in date_range(first_day, last_day, timedelta(days=1)):\n",
    "    #print(day)\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15.字符串转换为日期\n",
    "\n",
    "strptime() 的性能要比你想象中的差很多， 因为它是使用纯Python实现，并且必须处理所有的系统本地设置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2022, 6, 29, 0, 0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'2022-06-29 20:27:33'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "text = '2022-06-29'\n",
    "datetime.strptime(text, '%Y-%m-%d')\n",
    "\n",
    "datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 16.结合时区的日期操作\n",
    "\n",
    "对几乎所有涉及到时区的问题，你都应该使用 pytz 模块。这个包提供了Olson时区数据库， 它是时区信息的事实上的标准，在很多语言和操作系统里面都可以找到。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2012-12-21 09:30:00\n",
      "2012-12-21 09:30:00-06:00\n",
      "2012-12-21 23:30:00+08:00\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1656506564.099993"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "from pytz import timezone\n",
    "\n",
    "d = datetime(2012, 12, 21, 9, 30, 0)\n",
    "print(d)\n",
    "\n",
    "central = timezone('US/Central')\n",
    "loc_d = central.localize(d)\n",
    "print(loc_d)\n",
    "\n",
    "# 一旦日期被本地化了， 它就可以转换为其他时区的时间了\n",
    "sh_d = loc_d.astimezone(timezone('Asia/Shanghai'))\n",
    "print(sh_d)\n",
    "\n",
    "datetime.now().timestamp()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "vscode": {
   "interpreter": {
    "hash": "caf1c2fcf97217de91eafa76b907d50f9ea378f5ffbee7f571142d119bb6a771"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
